import { ipcMain } from 'electron';
import { getMainWindow } from 'ee-core/electron';

/**
 * 关闭窗口
 */
ipcMain.handle('close', () => {
  const win = getMainWindow();
  win.close();
});

/**
 * 窗口最小化
 */
ipcMain.handle('min', () => {
  const win = getMainWindow();
  win.minimize();
});

/**
 * 窗口最大化
 */
ipcMain.handle('max', () => {
  const win = getMainWindow();
  //判断窗口是否最大化
  if (win.isMaximized()) {
    win.restore(); //将窗口恢复为之前的状态
  } else {
    win.maximize(); //将窗口全屏
  }
});

// 处理窗口位置请求
ipcMain.handle('get-window-position', () => {
  const win = getMainWindow();
  const [x, y] = win.getPosition();
  return { x, y };
});

// 处理窗口移动
ipcMain.on('move-window', (event, { x, y }) => {
  const win = getMainWindow();
  win.setPosition(x, y, true);
});
